<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006 - 2008 The Bitsand Project (http://bitsand.googlecode.com/)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

include ('../inc/inc_head_db.php');
include ('../inc/inc_admin.php');
include ('../inc/inc_head_html.php');

$db_prefix = DB_PREFIX;
$key = CRYPT_KEY;

if ($_GET ['action'] == 'remove') {
	//Set up UPDATE query to remove meal ticket
	$pid = (int) $_GET ['pid'];
	$sql_update = "UPDATE {$db_prefix}bookings SET bkMealTicket = 0 WHERE bkPlayerID = $pid";
	ba_db_query ($link, $sql_update);
}

if ($_POST ['btnSubmit'] != '' && CheckReferrer ('admin_mealticket.php'))
foreach ($_POST as $key => $value) {
	if (substr ($key, 0, 7) == "hPlayer") {
		$meal = (int) $_POST ["chkPl{$value}"];
		$iPlayerID = (int) $value;
		$amountpaid = sanitiseAmount($_POST ["txtAmountPaid{$value}"]);
		$amountexpected = sanitiseAmount($_POST ["txtAmountExpected{$value}"]);
		$sql_update = "UPDATE {$db_prefix}bookings SET bkMealTicket = $meal, bkAmountPaid = $amountpaid, bkAmountExpected = $amountexpected WHERE bkPlayerID = " . $iPlayerID;
		ba_db_query ($link, $sql_update);
	}
}

//Get list of players that have confirmed their booking
$sql = "SELECT bkPlayerID, " . 
	"plFirstName, " .
	"plSurname, " .
	"plBookAs, " .
	"plBunkRequested, " .
	"bkMealTicket, " .
	"chName, " .
	"bkDateOOCConfirmed, " .
	"bkDateICConfirmed, " .
	"bkDatePaymentConfirmed, " .
	"bkAmountPaid, " .
	"bkAmountExpected " .
	"FROM {$db_prefix}players, {$db_prefix}characters, {$db_prefix}bookings " .
	"WHERE plPlayerID = chPlayerID AND chPlayerID = bkPlayerID ";
$result = ba_db_query ($link, $sql);
?>
<script src="../inc/sorttable.js" type="text/javascript"></script>

<script type="text/javascript">
	function changeAmounts(id, bookingtype)
	{
		<?php
			if (!is_null(PAYPAL_AMOUNT_P1)) {echo "var player1 = ".PAYPAL_AMOUNT_P1.";\n";}
			if (!is_null(PAYPAL_AMOUNT_P2)) {echo "var player2 = ".PAYPAL_AMOUNT_P2.";\n";}
			if (!is_null(PAYPAL_AMOUNT_S1)) {echo "var staff1 = ".PAYPAL_AMOUNT_S1.";\n";}
			if (!is_null(PAYPAL_AMOUNT_S2)) {echo "var staff2 = ".PAYPAL_AMOUNT_S2.";\n";}
			if (!is_null(PAYPAL_AMOUNT_M1)) {echo "var monster1 = ".PAYPAL_AMOUNT_M1.";\n";}
			if (!is_null(PAYPAL_AMOUNT_M2)) {echo "var monster2 = ".PAYPAL_AMOUNT_M2.";\n";}
		?>
		var meal = document.forms[0].elements['chkPl' + id].checked;

		var amount1 = 0;
		var amount2 = 0;

		if (bookingtype == 'Staff')
		{
			amount1 = staff1.toFixed(2);
			amount2 = staff2.toFixed(2);
		}
		else if (bookingtype == 'Monster')
		{
			amount1 = monster1.toFixed(2);
			amount2 = monster2.toFixed(2);
		}
		else
		{
			amount1 = player1.toFixed(2);
			amount2 = player2.toFixed(2);
		}

		if (meal)
		{
			document.forms[0].elements['txtAmountExpected' + id].value = amount2;
		}
		else
		{
			document.forms[0].elements['txtAmountExpected' + id].value = amount1;
		}
	}

</script>

<h1><?php echo TITLE?> - Meal Tickets</h1>

<p>
<a href = 'admin.php'>Admin</a>
</p>

<p>
The following people have confirmed a booking. If the Payment date is blank, they are not marked as paid. Click on a column header to sort by that column. To request or remove a meal ticket, tick or untick the relevant players boxes and click Submit.
</p>
<p>
The amount paid and amount expected values will be updated to the values they are currently set to.
</p>
<form action = 'admin_mealticket.php' method = 'post'>

<table border = '1' class="sortable">
<tr>
<th>Request Meal Ticket</th>
<th>Amount Paid</th>
<th>Amount Expected</th>
<th>Player ID</th>
<th>OOC First Name</th>
<th>OOC Surname</th>
<th>IC Name</th>
<th>Booking As</th>
<th>Date OOC Confirmed</th>
<th>Date IC Confirmed</th>
<th>Date Payment Confirmed</th>
<th>Meal Ticket Requested?</th>
</tr>

<?php
while ($row = ba_db_fetch_assoc ($result)) {
	echo "<tr class = 'highlight'><td class = 'mid'>";
	if ($row ['bkMealTicket'] == 1)
		$sChecked = ' checked';
	else
		$sChecked = '';
	echo "<input type = 'hidden' name = 'hPlayer" . $row ['bkPlayerID'] . "' value = '" . $row ['bkPlayerID'] . "'>";
	echo "<input type = 'checkbox' name = 'chkPl{$row ['bkPlayerID']}' value = '{$row ['bkPlayerID']}'{$sChecked} onClick=\"changeAmounts(". $row ['bkPlayerID'] .",'".$row ['plBookAs']."')\"></td>\n";
	echo "<td><input type = 'textbox' size = 5 name='txtAmountPaid" . $row ['bkPlayerID'] . "' value='".$row ['bkAmountPaid']."' /></td>\n";
	echo "<td><input type = 'textbox' size = 5 name='txtAmountExpected" . $row ['bkPlayerID'] . "' value='".$row ['bkAmountExpected']."' /></td>\n";

	echo "<td>" . PID_PREFIX . sprintf ('%03s', $row ['bkPlayerID']) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['plFirstName'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['plSurname'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['chName'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['plBookAs'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['bkDateOOCConfirmed'])) . "</td>\n";
	echo "<td>" . htmlentities (stripslashes ($row ['bkDateICConfirmed'])) . "</td>\n";

	if ($row ['bkDatePaymentConfirmed'] != '0000-00-00')
	{
		$PaymentDate = $row ['bkDatePaymentConfirmed']; 
	}
	else
	{
		$PaymentDate = '';
	}
	echo "<td>" . htmlentities (stripslashes ($PaymentDate)) . "</td>\n";
	if ($row ['bkMealTicket'] == 0)
		echo "<td>No</td>";
	else
		echo "<td>Yes</td>";
	
	echo "</tr>\n";
}
?>

</table>

<p>
<input type = 'submit' value = 'Submit' name = 'btnSubmit'>&nbsp;
<input type = 'reset' value = 'Reset'>
</p>
</form>

<?php
include ('../inc/inc_foot.php');
?>